<template>
    <el-drawer title="我是标题" :visible.sync="dialogVisible" :with-header="true" class="demand_box" size='600px' @close="resetForm('addPictureMultiSzForm')">
        <div class="flexbox flexcolumn height_100">
            <div class="demand_head flexbox flexcenter flexbetween">
                <span class="demand_title">创建多个需求</span>
                <span class="icon_btn icon_btn_32" @click="resetForm('addPictureMultiSzForm')"><img src="../../assets/img/close_check.svg" alt=""></span>
            </div>
            <div class="demand_center flex">
                <el-form label-position="top" style="margin-top:-10px" :model="addPictureMultiSzForm" size="medium" :hide-required-asterisk='true' ref="addPictureMultiSzForm" :rules="addPictureMultiSzRules" class="dialog_form">
                    <div v-if="step==1" key="1">
                        <el-form-item label="品牌/产品" prop="short_name">
                            <el-row :gutter="12">
                                <el-col :span="12">
                                    <el-form-item prop="brand_id">
                                        <el-select v-model="brandSelect" placeholder="请选择品牌" value-key='brand_id' style="width:100%" @change="selectBrand">
                                            <el-option :label="item.brand_name+filters.chanelName(item.channel)" :value="item" v-for="item in brandList" :key="item.brand_id">
                                                <span>{{item.brand_name}}{{filters.chanelName(item.channel)}}</span>
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item prop="product_id">
                                        <el-select v-model="addPictureMultiSzForm.product_id" placeholder="请选择产品" style="width:100%" :disabled="!addPictureMultiSzForm.brand_id" @change="selectProduct">
                                            <el-option :label="item.name" :value="item.product_id" v-for="item in productList" :key="item.product_id"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form-item>
                        <el-form-item label="业务场景" prop="business_scene">
                            <el-select v-model="addPictureMultiSzForm.business_scene" :disabled="!addPictureMultiSzForm.product_id" placeholder="请选择业务场景" style="width:100%">
                                <el-option :label="item.scene_name" :value="item.business_scene" v-for="item in sceneList" :key="item.business_scene"></el-option>
                            </el-select>
                        </el-form-item>
                    </div>
                    <div v-else key="2">
                        <div class="step_box flexbox flexcenter flexbetween">
                            <!-- <div class="flexbox flexcenter">
                                需求类型：
                                <span>剪辑需求</span>
                            </div> -->
                            <div class="flexbox flexcenter">
                                品牌/产品：
                                <span>{{branname}}/{{projectname}}</span>
                            </div>
                            <div class="flexbox flexcenter">
                                业务场景：
                                <span v-if="addPictureMultiSzForm.business_scene==1">短视频原创</span>
                                <span v-else-if="addPictureMultiSzForm.business_scene==2">短视频混剪</span>
                                <span v-else-if="addPictureMultiSzForm.business_scene==3">直播素材</span>
                                <span v-else-if="addPictureMultiSzForm.business_scene==4">直播直投</span>
                                <span v-else-if="addPictureMultiSzForm.business_scene==5">短视频</span>
                                <span v-else-if="addPictureMultiSzForm.business_scene==6">直播</span>
                            </div>
                        </div>
                        <div class="demand_item" v-for="(item,index) in addPictureMultiSzForm.demand_list" :key="index">
                            <div class="demand_top flexbox flexcenter flexbetween">
                                <span class="demand_num">需求 {{index+1}}</span>
                                <div class="demand_btn">
                                    <el-link type="danger" :underline="false" @click="deletList(index)" v-if="addPictureMultiSzForm.demand_list.length>1">删除</el-link>
                                    <el-link type="primary" :underline="false" @click="addList(item)">复制</el-link>
                                </div>
                            </div>
                            <el-row :gutter="12">
                                <el-col :span="12">
                                    <el-form-item :prop="`demand_list.${index}.designer_uid`" label="平面" :rules="{required: true, message: '请选择', trigger: 'change'}">
                                        <el-select v-model="item.designer_uid" placeholder="请选择平面" style="width:100%">
                                            <el-option :label="item.nickname" :value="item.id" v-for="item in optimiList" :key="item.id"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="截止日期" :prop="`demand_list.${index}.closing_date`" :rules="{required: true, message: '请选择', trigger: 'change'}">
                                        <el-select v-model="item.closing_date" placeholder="请选择该需求的截止日期" style="width:100%">
                                            <el-option label="周一" :value="1"></el-option>
                                            <el-option label="周二" :value="2"></el-option>
                                            <el-option label="周三" :value="3"></el-option>
                                            <el-option label="周四" :value="4"></el-option>
                                            <el-option label="周五" :value="5"></el-option>
                                            <el-option label="周六" :value="6"></el-option>
                                            <el-option label="周日" :value="7"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-form-item label="需求信息" :prop="`demand_list.${index}.description`">
                                <el-input type="textarea" :rows="3" placeholder="请输入需求信息" v-model="item.description">
                                </el-input>
                            </el-form-item>
                        </div>
                    </div>
                </el-form>
            </div>
            <div class="demand_footer flexbox flexcenter">
                <template v-if="step==1">
                    <el-button plain size="small" class="el-button-width" @click="resetForm('addPictureMultiSzForm')">取 消</el-button>
                    <el-button type="primary" size="small" class="el-button-width" @click="nextForm('addPictureMultiSzForm')">下一步</el-button>
                </template>
                <template v-else>
                    <el-button plain size="small" class="el-button-width" @click="step = 1">上一步</el-button>
                    <el-button type="primary" size="small" class="el-button-width" @click="submitForm('addPictureMultiSzForm')" :loading="loading" :disabled='loading'>完成创建</el-button>
                </template>
            </div>
        </div>
    </el-drawer>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            step: 1,
            loading: false,
            dialogVisible: false,
            brandSelect: {},
            brandList: [], //品牌列表
            productList: [], //产品列表
            sceneList: [], //业务场景列表
            scriptList: [],
            optimiList: [], //优化师列表
            branname: "",
            projectname: "",
            addPictureMultiSzForm: {
                year: "",
                week: "",
                brand_id: "",
                product_id: "",
                channel: "",
                business_scene: "",
                demand_list: [
                    {
                        description: "",
                        closing_date: "",
                        designer_uid: "",
                    },
                ],
            },
            addPictureMultiSzRules: {
                brand_id: [
                    {
                        required: true,
                        message: "请选择品牌",
                        trigger: "change",
                    },
                ],
                product_id: [
                    {
                        required: true,
                        message: "请选择产品",
                        trigger: "change",
                    },
                ],
                business_scene: [
                    {
                        required: true,
                        message: "请选择业务场景",
                        trigger: "change",
                    },
                ],
            },
        };
    },
    created() {
        this.$bus.on("addPictureMultiSz", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.addPictureMultiSzForm.week = value.week;
            this.addPictureMultiSzForm.year = value.year;
            this.getBrandList();
            this.getOptimization();
        });
    },
    beforeDestroy() {
        this.$bus.off("addPictureMultiSz");
    },
    methods: {
        //获取品牌列表
        async getBrandList() {
            const { data: res } = await this.$http.post(
                "adminapi/project/brandList"
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.brandList = res.data;
            }
        },

        // 获取产品
        async getProductList() {
            const { data: res } = await this.$http.post(
                "adminapi/project/productList",
                {
                    brand_id: this.addPictureMultiSzForm.brand_id,
                }
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.productList = res.data;
            }
        },

        // 获取业务场景
        async getSceneList() {
            const { data: res } = await this.$http.post(
                "adminapi/project/sceneList",
                {
                    brand_id: this.addPictureMultiSzForm.brand_id,
                    product_id: this.addPictureMultiSzForm.product_id,
                }
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.sceneList = res.data;
            }
        },

        // 获取优化师列表
        async getOptimization() {
            const { data: res } = await this.$http.post(
                "adminapi/company/szGraphicList"
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.optimiList = res.data;
            }
        },

        //品牌选择
        selectBrand() {
            this.addPictureMultiSzForm.brand_id = this.brandSelect.brand_id;
            this.addPictureMultiSzForm.channel = this.brandSelect.channel;
            this.addPictureMultiSzForm.product_id = "";
            this.addPictureMultiSzForm.business_scene = "";
            this.getProductList();
        },

        // 产品选择
        selectProduct() {
            this.addPictureMultiSzForm.business_scene = "";
            this.getSceneList();
        },

        // 下一步
        nextForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.step = 2;
                    this.branname = this.brandName();
                    this.projectname = this.projectName();
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true;
                    const { data: res } = await this.$http.post(
                        "adminapi/demandsz/pictureBatchAdd",
                        this.addPictureMultiSzForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "创建成功",
                            type: "success",
                        });
                        this.loading = false;
                        this.resetForm(formName);
                        this.$parent.getDataList(1);
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        // 添加列表
        addList(item) {
            this.addPictureMultiSzForm.demand_list.push({
                designer_uid: item.designer_uid,
                closing_date: item.closing_date,
                description: item.description,
            });
        },

        // 删除列表
        deletList(index) {
            this.addPictureMultiSzForm.demand_list.splice(index, 1);
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.brandSelect = {};
            this.step = 1;
            this.addPictureMultiSzForm = {
                year: "",
                week: "",
                brand_id: "",
                product_id: "",
                channel: "",
                business_scene: "",
                demand_list: [
                    {
                        description: "",
                        closing_date: "",
                        designer_uid: "",
                    },
                ],
            };
            this.dialogVisible = false;
        },

        brandName() {
            let arr = this.brandList.filter((item) => {
                if (item.brand_id == this.addPictureMultiSzForm.brand_id) {
                    return item;
                }
            });
            return arr[0].brand_name;
        },

        projectName() {
            let arr = this.productList.filter((item) => {
                if (item.product_id == this.addPictureMultiSzForm.product_id) {
                    return item;
                }
            });
            return arr[0].name;
        },
    },
    computed: {},
    components: {},
};
</script>

<style scoped>
.step_box {
    height: 48px;
    background: rgba(165, 177, 209, 0.08);
    border-radius: 2px;
    padding: 0 20px;
    font-size: 14px;
    color: #616c85;
    margin-bottom: 20px;
    margin-top: 10px;
}
.step_box span {
    color: #17233d;
    max-width: 230px;
    overflow: hidden;
    height: 14px;
    line-height: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.demand_num {
    font-size: 14px;
    color: #b4b7bd;
}
.demand_top {
    margin-bottom: 10px;
}
.demand_item {
    padding: 10px 20px 0;
    border: 1px solid #ebebeb;
    border-radius: 2px;
    margin-top: 12px;
}
/deep/ .el-drawer__header{
    height: 0;
    width: 0;
    padding: 0;
    margin: 0;
    overflow: hidden;
}
</style>


